import 'package:flutter/material.dart';
import 'package:lifecycle_android_flutter_plugin/lifecycle_state.dart';

void main() {
  runApp(
    MaterialApp(
      title: "Flutter Lifecycle Demo",
      home: FromWidget(),
      routes: {
        "to": (context) => ToWidget(),
      },
      navigatorObservers: [LifecycleState.routeObserver],
    ),
  );
}

class FromWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _FromState();
  }
}

class _FromState extends LifecycleState<FromWidget> {
  @override
  void onPause() {
    super.onPause();
    print("onPause");
  }

  @override
  void onCreate() {
    super.onCreate();
    print("onCreate");
  }

  @override
  void onResume() {
    super.onResume();
    print("onResume");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Center(
        child: SizedBox(
          width: 120,
          height: 80,
          child: ElevatedButton(
            onPressed: () {
              Navigator.of(context).pushNamed("to");
            },
            child: const Text(
              "点我跳转",
              style: TextStyle(fontSize: 20),
            ),
          ),
        ),
      ),
    );
  }
}

class ToWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _ToState();
  }
}

class _ToState extends LifecycleState<ToWidget> {
  @override
  void onDestroy() {
    super.onDestroy();
    print("onDestroy");
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Center(
        child: SizedBox(
          width: 120,
          height: 80,
          child: ElevatedButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: const Text(
              "点我返回",
              style: TextStyle(fontSize: 20),
            ),
          ),
        ),
      ),
    );
  }
}
